package org.one.stone.soup.grfxML.plugin.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.one.stone.soup.grfxML.DataColor;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.swing.JColorEditor;

public class JColorFieldPlugin extends SimplePlugin implements Runnable,ChangeListener{
	private static final int MESSAGE_ARG = 0;
	private static final int TARGET_ARG = 1;
	private static final int ENTER_ARG = 2;

	private DataString message = new DataString();
	private DataColor target = new DataColor();
	private DataState enter = new DataState();

	private JColorEditor editor;
/**
 * FileToText constructor comment.
 */
public JColorFieldPlugin(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize() {}
/**
 * process method comment.
 */
public void process()
{
 try{
	if(enter.getValue()==true)
	{
		new Thread(this).start();
		enter.setValue(false,this);
	}
 }catch(LockException le)
 {}
}

public void run()
{
	//try{
		Color color = new Color( target.getValueX(),target.getValueY(),target.getValueZ(),target.getValueT());

		Component parent = getEngine().getController().getComponent();
		while(parent!=null && !(parent instanceof Frame))
		{
			parent = parent.getParent();
		}

		editor = new JColorEditor((Frame)parent,color);
		editor.addChangeListener(this);
		editor.setTitle( message.getValue() );
		editor.setVisible(true);
		editor.dispose();

		/*if(color!=null)
		{
			target.setValueX( color.getRed(),this );
			target.setValueY( color.getGreen(),this );
			target.setValueZ( color.getBlue(),this );
			target.setValueT( color.getAlpha(),this );
		}*/
	//}catch(LockException le)
	//{}
}

public void stateChanged(ChangeEvent ce)
{
	try{
		Color color = editor.getColor();

		target.setValueX( color.getRed(),this );
		target.setValueY( color.getGreen(),this );
		target.setValueZ( color.getBlue(),this );
		target.setValueT( color.getAlpha(),this );
	}catch(LockException le)
	{}
}

/**
 * register method comment.
 */
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	message = grfxMLCaster.cast(message,getArg(message,MESSAGE_ARG,store));
	target = grfxMLCaster.cast(target,getArg(target,TARGET_ARG,store));
	enter = grfxMLCaster.cast(enter,getArg(enter,ENTER_ARG,store));
}
public void replace(Field oldObj, Field newObj)
{
	message = grfxMLCaster.replace(message,oldObj,newObj);
	target = grfxMLCaster.replace(target,oldObj,newObj);
	enter = grfxMLCaster.replace(enter,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
